package com.wonderli.example.sensortest;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class MagneticfieldSensor extends Activity{
	private Sensor sensor;
    private SensorManager smgr;    
    private SensorEventListener sl;
    //private TextView tvOrientation;
    private TextView tvAcceler;
    //private TextView tvAccuracy;
    private final String TAG = getClass().getName();
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		//tvOrientation = (TextView) findViewById(R.id.accelerometer_orientation);
		//tvAccuracy = (TextView) findViewById(R.id.accelerometer_accuracy);		 
        smgr = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensor = smgr.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); 		
        sl = new SensorEventListener(){			
			public void onAccuracyChanged(Sensor sensor, int accuracy) {					
			
			}
			public void onSensorChanged(SensorEvent event) {				
				float dataX = (float) event.values[SensorManager.DATA_X];
				float dataY = (float) event.values[SensorManager.DATA_Y];
				float dataZ = (float) event.values[SensorManager.DATA_Z];
				System.out.println("X: " + dataX + " Y: " + dataY + " Z: " + dataZ);
				tvAcceler = (TextView) findViewById(R.id.magneticfield_magneticfield);
				tvAcceler.setText("X = " + dataX + ", \nY = " + dataY + ", \nZ = " + dataZ);			
			}        	
        };
        setContentView(R.layout.magneticfield);		
	}
	public void onResume(){
		super.onResume();
		smgr.registerListener(sl, sensor, SensorManager.SENSOR_DELAY_FASTEST);
	}
	public void onStop(){
		smgr.unregisterListener(sl, sensor);
		super.onStop();
	}
	
	


}
